home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Modules / parsermodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  64.5 KB  |  2,627 lines  |  [TEXT/CWIE]

  1. /*  parsermodule.c
  2.  *
  3.  *  Copyright 1995-1996 by Fred L. Drake, Jr. and Virginia Polytechnic
  4.  *  Institute and State University, Blacksburg, Virginia, USA.
  5.  *  Portions copyright 1991-1995 by Stichting Mathematisch Centrum,
  6.  *  Amsterdam, The Netherlands.  Copying is permitted under the terms
  7.  *  associated with the main Python distribution, with the additional
  8.  *  restriction that this additional notice be included and maintained
  9.  *  on all distributed copies.
  10.  *
  11.  *  This module serves to replace the original parser module written
  12.  *  by Guido.  The functionality is not matched precisely, but the
  13.  *  original may be implemented on top of this.  This is desirable
  14.  *  since the source of the text to be parsed is now divorced from
  15.  *  this interface.
  16.  *
  17.  *  Unlike the prior interface, the ability to give a parse tree
  18.  *  produced by Python code as a tuple to the compiler is enabled by
  19.  *  this module.  See the documentation for more details.
  20.  */
  21.  
  22. #include "Python.h"            /* general Python API          */
  23. #include "graminit.h"            /* symbols defined in the grammar */
  24. #include "node.h"            /* internal parser structure      */
  25. #include "token.h"            /* token definitions          */
  26.                     /* ISTERMINAL() / ISNONTERMINAL() */
  27. #include "compile.h"            /* PyNode_Compile()          */
  28.  
  29. /*
  30.  *  All the "fudge" declarations are here:
  31.  *
  32.  *  This isn't part of the Python runtime, but it's in the library somewhere.
  33.  *  Where it is varies a bit, so just declare it.  Don't use any prototype;
  34.  *  different systems declare it a little differently, and we don't need the
  35.  *  extra warnings.
  36.  */
  37. extern char* strdup();
  38.  
  39.  
  40. /*  String constants used to initialize module attributes.
  41.  *
  42.  */
  43. static char*
  44. parser_copyright_string
  45. = "Copyright 1995-1996 by Virginia Polytechnic Institute & State\n\
  46. University, Blacksburg, Virginia, USA, and Fred L. Drake, Jr., Reston,\n\
  47. Virginia, USA.  Portions copyright 1991-1995 by Stichting Mathematisch\n\
  48. Centrum, Amsterdam, The Netherlands.";
  49.  
  50.  
  51. static char*
  52. parser_doc_string
  53. = "This is an interface to Python's internal parser.";
  54.  
  55. static char*
  56. parser_version_string = "0.4";
  57.  
  58.  
  59. typedef PyObject* (*SeqMaker) Py_PROTO((int length));
  60. typedef void (*SeqInserter) Py_PROTO((PyObject* sequence,
  61.                       int index,
  62.                       PyObject* element));
  63.  
  64. /*  The function below is copyrigthed by Stichting Mathematisch Centrum.  The
  65.  *  original copyright statement is included below, and continues to apply
  66.  *  in full to the function immediately following.  All other material is
  67.  *  original, copyrighted by Fred L. Drake, Jr. and Virginia Polytechnic
  68.  *  Institute and State University.  Changes were made to comply with the
  69.  *  new naming conventions.  Added arguments to provide support for creating
  70.  *  lists as well as tuples, and optionally including the line numbers.
  71.  */
  72.  
  73. /***********************************************************
  74. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  75. The Netherlands.
  76.  
  77.                         All Rights Reserved
  78.  
  79. Permission to use, copy, modify, and distribute this software and its
  80. documentation for any purpose and without fee is hereby granted,
  81. provided that the above copyright notice appear in all copies and that
  82. both that copyright notice and this permission notice appear in
  83. supporting documentation, and that the names of Stichting Mathematisch
  84. Centrum or CWI or Corporation for National Research Initiatives or
  85. CNRI not be used in advertising or publicity pertaining to
  86. distribution of the software without specific, written prior
  87. permission.
  88.  
  89. While CWI is the initial source for this software, a modified version
  90. is made available by the Corporation for National Research Initiatives
  91. (CNRI) at the Internet address ftp://ftp.python.org.
  92.  
  93. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  94. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  95. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  96. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  97. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  98. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  99. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  100. PERFORMANCE OF THIS SOFTWARE.
  101.  
  102. ******************************************************************/
  103.  
  104. static PyObject*
  105. node2tuple(n, mkseq, addelem, lineno)
  106.      node *n;                /* node to convert         */
  107.      SeqMaker mkseq;            /* create sequence         */
  108.      SeqInserter addelem;        /* func. to add elem. in seq.     */
  109.      int lineno;            /* include line numbers?     */
  110. {
  111.     if (n == NULL) {
  112.     Py_INCREF(Py_None);
  113.     return Py_None;
  114.     }
  115.     if (ISNONTERMINAL(TYPE(n))) {
  116.     int i;
  117.     PyObject *v, *w;
  118.     v = mkseq(1 + NCH(n));
  119.     if (v == NULL)
  120.         return v;
  121.     w = PyInt_FromLong(TYPE(n));
  122.     if (w == NULL) {
  123.         Py_DECREF(v);
  124.         return NULL;
  125.     }
  126.     addelem(v, 0, w);
  127.     for (i = 0; i < NCH(n); i++) {
  128.         w = node2tuple(CHILD(n, i), mkseq, addelem, lineno);
  129.         if (w == NULL) {
  130.         Py_DECREF(v);
  131.         return NULL;
  132.         }
  133.         addelem(v, i+1, w);
  134.     }
  135.     return (v);
  136.     }
  137.     else if (ISTERMINAL(TYPE(n))) {
  138.     PyObject *result = mkseq(2 + lineno);
  139.     if (result != NULL) {
  140.         addelem(result, 0, PyInt_FromLong(TYPE(n)));
  141.         addelem(result, 1, PyString_FromString(STR(n)));
  142.         if (lineno == 1)
  143.         addelem(result, 2, PyInt_FromLong(n->n_lineno));
  144.     }
  145.     return (result);
  146.     }
  147.     else {
  148.     PyErr_SetString(PyExc_SystemError,
  149.             "unrecognized parse tree node type");
  150.     return NULL;
  151.     }
  152. }   /* node2tuple() */
  153. /*
  154.  *  End of material copyrighted by Stichting Mathematisch Centrum.
  155.  */
  156.  
  157.  
  158.  
  159. /*  There are two types of intermediate objects we're interested in:
  160.  *  'eval' and 'exec' types.  These constants can be used in the ast_type
  161.  *  field of the object type to identify which any given object represents.
  162.  *  These should probably go in an external header to allow other extensions
  163.  *  to use them, but then, we really should be using C++ too.  ;-)
  164.  *
  165.  *  The PyAST_FRAGMENT type is not currently supported.  Maybe not useful?
  166.  *  Haven't decided yet.
  167.  */
  168.  
  169. #define PyAST_EXPR    1
  170. #define PyAST_SUITE    2
  171. #define PyAST_FRAGMENT    3
  172.  
  173.  
  174. /*  These are the internal objects and definitions required to implement the
  175.  *  AST type.  Most of the internal names are more reminiscent of the 'old'
  176.  *  naming style, but the code uses the new naming convention.
  177.  */
  178.  
  179. static PyObject*
  180. parser_error = 0;
  181.  
  182.  
  183. typedef struct _PyAST_Object {
  184.  
  185.     PyObject_HEAD            /* standard object header        */
  186.     node* ast_node;            /* the node* returned by the parser */
  187.     int      ast_type;            /* EXPR or SUITE ?            */
  188.  
  189. } PyAST_Object;
  190.  
  191.  
  192. staticforward void parser_free Py_PROTO((PyAST_Object *ast));
  193. staticforward int  parser_compare Py_PROTO((PyAST_Object *left,
  194.                         PyAST_Object *right));
  195.  
  196.  
  197. /* static */
  198. PyTypeObject PyAST_Type = {
  199.  
  200.     PyObject_HEAD_INIT(&PyType_Type)
  201.     0,
  202.     "ast",                /* tp_name        */
  203.     sizeof(PyAST_Object),        /* tp_basicsize        */
  204.     0,                    /* tp_itemsize        */
  205.     (destructor)parser_free,        /* tp_dealloc        */
  206.     0,                    /* tp_print        */
  207.     0,                    /* tp_getattr        */
  208.     0,                    /* tp_setattr        */
  209.     (cmpfunc)parser_compare,        /* tp_compare        */
  210.     0,                    /* tp_repr        */
  211.     0,                    /* tp_as_number        */
  212.     0,                    /* tp_as_sequence    */
  213.     0,                    /* tp_as_mapping    */
  214.     0,                    /* tp_hash        */
  215.     0,                    /* tp_call        */
  216.     0                    /* tp_str        */
  217.  
  218. };  /* PyAST_Type */
  219.  
  220.  
  221. static int
  222. parser_compare_nodes(left, right)
  223.      node *left;
  224.      node *right;
  225. {
  226.     int j;
  227.  
  228.     if (TYPE(left) < TYPE(right))
  229.     return (-1);
  230.  
  231.     if (TYPE(right) < TYPE(left))
  232.     return (1);
  233.  
  234.     if (ISTERMINAL(TYPE(left)))
  235.     return (strcmp(STR(left), STR(right)));
  236.  
  237.     if (NCH(left) < NCH(right))
  238.     return (-1);
  239.  
  240.     if (NCH(right) < NCH(left))
  241.     return (1);
  242.  
  243.     for (j = 0; j < NCH(left); ++j) {
  244.     int v = parser_compare_nodes(CHILD(left, j), CHILD(right, j));
  245.  
  246.     if (v)
  247.         return (v);
  248.     }
  249.     return (0);
  250.  
  251. }   /* parser_compare_nodes() */
  252.  
  253.  
  254. /*  int parser_compare(PyAST_Object* left, PyAST_Object* right)
  255.  *
  256.  *  Comparison function used by the Python operators ==, !=, <, >, <=, >=
  257.  *  This really just wraps a call to parser_compare_nodes() with some easy
  258.  *  checks and protection code.
  259.  *
  260.  */
  261. static int
  262. parser_compare(left, right)
  263.      PyAST_Object *left;
  264.      PyAST_Object *right;
  265. {
  266.     if (left == right)
  267.     return (0);
  268.  
  269.     if ((left == 0) || (right == 0))
  270.     return (-1);
  271.  
  272.     return (parser_compare_nodes(left->ast_node, right->ast_node));
  273.  
  274. }   /* parser_compare() */
  275.  
  276.  
  277. /*  parser_newastobject(node* ast)
  278.  *
  279.  *  Allocates a new Python object representing an AST.  This is simply the
  280.  *  'wrapper' object that holds a node* and allows it to be passed around in
  281.  *  Python code.
  282.  *
  283.  */
  284. static PyObject*
  285. parser_newastobject(ast, type)
  286.      node *ast;
  287.      int type;
  288. {
  289.     PyAST_Object* o = PyObject_NEW(PyAST_Object, &PyAST_Type);
  290.  
  291.     if (o != 0) {
  292.     o->ast_node = ast;
  293.     o->ast_type = type;
  294.     }
  295.     return ((PyObject*)o);
  296.  
  297. }   /* parser_newastobject() */
  298.  
  299.  
  300. /*  void parser_free(PyAST_Object* ast)
  301.  *
  302.  *  This is called by a del statement that reduces the reference count to 0.
  303.  *
  304.  */
  305. static void
  306. parser_free(ast)
  307.      PyAST_Object *ast;
  308. {
  309.     PyNode_Free(ast->ast_node);
  310.     PyMem_DEL(ast);
  311.  
  312. }   /* parser_free() */
  313.  
  314.  
  315. /*  parser_ast2tuple(PyObject* self, PyObject* args)
  316.  *
  317.  *  This provides conversion from a node* to a tuple object that can be
  318.  *  returned to the Python-level caller.  The AST object is not modified.
  319.  *
  320.  */
  321. static PyObject*
  322. parser_ast2tuple(self, args)
  323.      PyObject *self;
  324.      PyObject *args;
  325. {
  326.     PyObject *ast;
  327.     PyObject *line_option = 0;
  328.     PyObject *res = 0;
  329.  
  330.     if (PyArg_ParseTuple(args, "O!|O:ast2tuple", &PyAST_Type, &ast,
  331.              &line_option)) {
  332.     int lineno = 0;
  333.     if (line_option != 0) {
  334.         lineno = PyObject_IsTrue(line_option);
  335.         lineno = lineno ? 1 : 0;
  336.     }
  337.     /*
  338.      *  Convert AST into a tuple representation.  Use Guido's function,
  339.      *  since it's known to work already.
  340.      */
  341.     res = node2tuple(((PyAST_Object*)ast)->ast_node,
  342.              PyTuple_New, PyTuple_SetItem, lineno);
  343.     }
  344.     return (res);
  345.  
  346. }   /* parser_ast2tuple() */
  347.  
  348.  
  349. /*  parser_ast2tuple(PyObject* self, PyObject* args)
  350.  *
  351.  *  This provides conversion from a node* to a tuple object that can be
  352.  *  returned to the Python-level caller.  The AST object is not modified.
  353.  *
  354.  */
  355. static PyObject*
  356. parser_ast2list(self, args)
  357.      PyObject *self;
  358.      PyObject *args;
  359. {
  360.     PyObject *ast;
  361.     PyObject *line_option = 0;
  362.     PyObject *res = 0;
  363.  
  364.     if (PyArg_ParseTuple(args, "O!|O:ast2list", &PyAST_Type, &ast,
  365.              &line_option)) {
  366.     int lineno = 0;
  367.     if (line_option != 0) {
  368.         lineno = PyObject_IsTrue(line_option);
  369.         lineno = lineno ? 1 : 0;
  370.     }
  371.     /*
  372.      *  Convert AST into a tuple representation.  Use Guido's function,
  373.      *  since it's known to work already.
  374.      */
  375.     res = node2tuple(((PyAST_Object*)ast)->ast_node,
  376.              PyList_New, PyList_SetItem, lineno);
  377.     }
  378.     return (res);
  379.  
  380. }   /* parser_ast2list() */
  381.  
  382.  
  383. /*  parser_compileast(PyObject* self, PyObject* args)
  384.  *
  385.  *  This function creates code objects from the parse tree represented by
  386.  *  the passed-in data object.  An optional file name is passed in as well.
  387.  *
  388.  */
  389. static PyObject*
  390. parser_compileast(self, args)
  391.      PyObject *self;
  392.      PyObject *args;
  393. {
  394.     PyAST_Object* ast;
  395.     PyObject*     res = 0;
  396.     char*      str = "<ast>";
  397.  
  398.     if (PyArg_ParseTuple(args, "O!|s", &PyAST_Type, &ast, &str))
  399.     res = (PyObject*) PyNode_Compile(ast->ast_node, str);
  400.  
  401.     return (res);
  402.  
  403. }   /* parser_compileast() */
  404.  
  405.  
  406. /*  PyObject* parser_isexpr(PyObject* self, PyObject* args)
  407.  *  PyObject* parser_issuite(PyObject* self, PyObject* args)
  408.  *
  409.  *  Checks the passed-in AST object to determine if it is an expression or
  410.  *  a statement suite, respectively.  The return is a Python truth value.
  411.  *
  412.  */
  413. static PyObject*
  414. parser_isexpr(self, args)
  415.      PyObject *self;
  416.      PyObject *args;
  417. {
  418.     PyAST_Object* ast;
  419.     PyObject* res = 0;
  420.  
  421.     if (PyArg_ParseTuple(args, "O!:isexpr", &PyAST_Type, &ast)) {
  422.     /*
  423.      *  Check to see if the AST represents an expression or not.
  424.      */
  425.     res = (ast->ast_type == PyAST_EXPR) ? Py_True : Py_False;
  426.     Py_INCREF(res);
  427.     }
  428.     return (res);
  429.  
  430. }   /* parser_isexpr() */
  431.  
  432.  
  433. static PyObject*
  434. parser_issuite(self, args)
  435.      PyObject *self;
  436.      PyObject *args;
  437. {
  438.     PyAST_Object* ast;
  439.     PyObject* res = 0;
  440.  
  441.     if (PyArg_ParseTuple(args, "O!:issuite", &PyAST_Type, &ast)) {
  442.     /*
  443.      *  Check to see if the AST represents an expression or not.
  444.      */
  445.     res = (ast->ast_type == PyAST_EXPR) ? Py_False : Py_True;
  446.     Py_INCREF(res);
  447.     }
  448.     return (res);
  449.  
  450. }   /* parser_issuite() */
  451.  
  452.  
  453. /*  err_string(char* message)
  454.  *
  455.  *  Sets the error string for an exception of type ParserError.
  456.  *
  457.  */
  458. static void
  459. err_string(message)
  460.      char *message;
  461. {
  462.     PyErr_SetString(parser_error, message);
  463.  
  464. }  /* err_string() */
  465.  
  466.  
  467. /*  PyObject* parser_do_parse(PyObject* args, int type)
  468.  *
  469.  *  Internal function to actually execute the parse and return the result if
  470.  *  successful, or set an exception if not.
  471.  *
  472.  */
  473. static PyObject*
  474. parser_do_parse(args, type)
  475.      PyObject *args;
  476.      int type;
  477. {
  478.     char*     string = 0;
  479.     PyObject* res    = 0;
  480.  
  481.     if (PyArg_ParseTuple(args, "s", &string)) {
  482.     node* n = PyParser_SimpleParseString(string,
  483.                          (type == PyAST_EXPR)
  484.                          ? eval_input : file_input);
  485.  
  486.     if (n != 0)
  487.         res = parser_newastobject(n, type);
  488.     else
  489.         err_string("Could not parse string.");
  490.     }
  491.     return (res);
  492.  
  493. }  /* parser_do_parse() */
  494.  
  495.  
  496. /*  PyObject* parser_expr(PyObject* self, PyObject* args)
  497.  *  PyObject* parser_suite(PyObject* self, PyObject* args)
  498.  *
  499.  *  External interfaces to the parser itself.  Which is called determines if
  500.  *  the parser attempts to recognize an expression ('eval' form) or statement
  501.  *  suite ('exec' form).  The real work is done by parser_do_parse() above.
  502.  *
  503.  */
  504. static PyObject*
  505. parser_expr(self, args)
  506.      PyObject *self;
  507.      PyObject *args;
  508. {
  509.     return (parser_do_parse(args, PyAST_EXPR));
  510.  
  511. }   /* parser_expr() */
  512.  
  513.  
  514. static PyObject*
  515. parser_suite(self, args)
  516.      PyObject *self;
  517.      PyObject *args;
  518. {
  519.     return (parser_do_parse(args, PyAST_SUITE));
  520.  
  521. }   /* parser_suite() */
  522.  
  523.  
  524.  
  525. /*  This is the messy part of the code.  Conversion from a tuple to an AST
  526.  *  object requires that the input tuple be valid without having to rely on
  527.  *  catching an exception from the compiler.  This is done to allow the
  528.  *  compiler itself to remain fast, since most of its input will come from
  529.  *  the parser directly, and therefore be known to be syntactically correct.
  530.  *  This validation is done to ensure that we don't core dump the compile
  531.  *  phase, returning an exception instead.
  532.  *
  533.  *  Two aspects can be broken out in this code:  creating a node tree from
  534.  *  the tuple passed in, and verifying that it is indeed valid.  It may be
  535.  *  advantageous to expand the number of AST types to include funcdefs and
  536.  *  lambdadefs to take advantage of the optimizer, recognizing those ASTs
  537.  *  here.  They are not necessary, and not quite as useful in a raw form.
  538.  *  For now, let's get expressions and suites working reliably.
  539.  */
  540.  
  541.  
  542. staticforward node* build_node_tree Py_PROTO((PyObject *tuple));
  543. staticforward int   validate_expr_tree Py_PROTO((node *tree));
  544. staticforward int   validate_file_input Py_PROTO((node *tree));
  545.  
  546.  
  547. /*  PyObject* parser_tuple2ast(PyObject* self, PyObject* args)
  548.  *
  549.  *  This is the public function, called from the Python code.  It receives a
  550.  *  single tuple object from the caller, and creates an AST object if the
  551.  *  tuple can be validated.  It does this by checking the first code of the
  552.  *  tuple, and, if acceptable, builds the internal representation.  If this
  553.  *  step succeeds, the internal representation is validated as fully as
  554.  *  possible with the various validate_*() routines defined below.
  555.  *
  556.  *  This function must be changed if support is to be added for PyAST_FRAGMENT
  557.  *  AST objects.
  558.  *
  559.  */
  560. static PyObject*
  561. parser_tuple2ast(self, args)
  562.      PyObject *self;
  563.      PyObject *args;
  564. {
  565.     PyObject *ast = 0;
  566.     PyObject *tuple = 0;
  567.     PyObject *temp = 0;
  568.     int ok;
  569.     int start_sym;
  570.  
  571.     if (!PyArg_ParseTuple(args, "O:tuple2ast", &tuple))
  572.     return (0);
  573.     if (!PySequence_Check(tuple)) {
  574.     PyErr_SetString(PyExc_ValueError,
  575.             "tuple2ast() requires a single sequence argument");
  576.     return (0);
  577.     }
  578.     /*
  579.      *    This mess of tests is written this way so we can use the abstract
  580.      *    object interface (AOI).  Unfortunately, the AOI increments reference
  581.      *    counts, which requires that we store a pointer to retrieved object
  582.      *    so we can DECREF it after the check.  But we really should accept
  583.      *    lists as well as tuples at the very least.
  584.      */
  585.     ok = PyObject_Length(tuple) >= 2;
  586.     if (ok) {
  587.     temp = PySequence_GetItem(tuple, 0);
  588.     ok = (temp != NULL) && PyInt_Check(temp);
  589.     if (ok)
  590.         /* this is used after the initial checks: */
  591.         start_sym = PyInt_AsLong(temp);
  592.     Py_XDECREF(temp);
  593.     }
  594.     if (ok) {
  595.     temp = PySequence_GetItem(tuple, 1);
  596.     ok = (temp != NULL) && PySequence_Check(temp);
  597.     Py_XDECREF(temp);
  598.     }
  599.     if (ok) {
  600.     temp = PySequence_GetItem(tuple, 1);
  601.     ok = (temp != NULL) && PyObject_Length(temp) >= 2;
  602.     if (ok) {
  603.         PyObject *temp2 = PySequence_GetItem(temp, 0);
  604.         if (temp2 != NULL) {
  605.         ok = PyInt_Check(temp2);
  606.         Py_DECREF(temp2);
  607.         }
  608.     }
  609.     Py_XDECREF(temp);
  610.     }
  611.     /* If we've failed at some point, get out of here. */
  612.     if (!ok) {
  613.     err_string("malformed sequence for tuple2ast()");
  614.     return (0);
  615.     }
  616.     /*
  617.      *  This might be a valid parse tree, but let's do a quick check
  618.      *  before we jump the gun.
  619.      */
  620.     if (start_sym == eval_input) {
  621.     /*  Might be an eval form.  */
  622.     node* expression = build_node_tree(tuple);
  623.  
  624.     if ((expression != 0) && validate_expr_tree(expression))
  625.         ast = parser_newastobject(expression, PyAST_EXPR);
  626.     }
  627.     else if (start_sym == file_input) {
  628.     /*  This looks like an exec form so far.  */
  629.     node* suite_tree = build_node_tree(tuple);
  630.  
  631.     if ((suite_tree != 0) && validate_file_input(suite_tree))
  632.         ast = parser_newastobject(suite_tree, PyAST_SUITE);
  633.     }
  634.     else
  635.     /*  This is a fragment, and is not yet supported.  Maybe they
  636.      *  will be if I find a use for them.
  637.      */
  638.     err_string("Fragmentary parse trees not supported.");
  639.  
  640.     /*  Make sure we throw an exception on all errors.  We should never
  641.      *  get this, but we'd do well to be sure something is done.
  642.      */
  643.     if ((ast == 0) && !PyErr_Occurred())
  644.     err_string("Unspecified ast error occurred.");
  645.  
  646.     return (ast);
  647.  
  648. }   /* parser_tuple2ast() */
  649.  
  650.  
  651. /*  int check_terminal_tuple()
  652.  *
  653.  *  Check a tuple to determine that it is indeed a valid terminal
  654.  *  node.  The node is known to be required as a terminal, so we throw
  655.  *  an exception if there is a failure.
  656.  *
  657.  *  The format of an acceptable terminal tuple is "(is[i])": the fact
  658.  *  that elem is a tuple and the integer is a valid terminal symbol
  659.  *  has been established before this function is called.  We must
  660.  *  check the length of the tuple and the type of the second element
  661.  *  and optional third element.  We do *NOT* check the actual text of
  662.  *  the string element, which we could do in many cases.  This is done
  663.  *  by the validate_*() functions which operate on the internal
  664.  *  representation.
  665.  */
  666. static int
  667. check_terminal_tuple(elem)
  668.      PyObject *elem;
  669. {
  670.     int   len = PyObject_Length(elem);
  671.     int   res = 1;
  672.     char* str = "Illegal terminal symbol; bad node length.";
  673.  
  674.     if ((len == 2) || (len == 3)) {
  675.     PyObject *temp = PySequence_GetItem(elem, 1);
  676.     res = PyString_Check(temp);
  677.     str = "Illegal terminal symbol; expected a string.";
  678.     if (res && (len == 3)) {
  679.         PyObject* third = PySequence_GetItem(elem, 2);
  680.         res = PyInt_Check(third);
  681.         str = "Invalid third element of terminal node.";
  682.         Py_XDECREF(third);
  683.     }
  684.     Py_XDECREF(temp);
  685.     }
  686.     else {
  687.     res = 0;
  688.     }
  689.     if (!res) {
  690.     elem = Py_BuildValue("(os)", elem, str);
  691.     PyErr_SetObject(parser_error, elem);
  692.     }
  693.     return (res);
  694.  
  695. }   /* check_terminal_tuple() */
  696.  
  697.  
  698. /*  node* build_node_children()
  699.  *
  700.  *  Iterate across the children of the current non-terminal node and build
  701.  *  their structures.  If successful, return the root of this portion of
  702.  *  the tree, otherwise, 0.  Any required exception will be specified already,
  703.  *  and no memory will have been deallocated.
  704.  *
  705.  */
  706. static node*
  707. build_node_children(tuple, root, line_num)
  708.      PyObject *tuple;
  709.      node *root;
  710.      int *line_num;
  711. {
  712.     int len = PyObject_Length(tuple);
  713.     int i;
  714.  
  715.     for (i = 1; i < len; ++i) {
  716.     /* elem must always be a tuple, however simple */
  717.     PyObject* elem = PySequence_GetItem(tuple, i);
  718.     int ok = elem != NULL;
  719.     long  type = 0;
  720.     char *strn = 0;
  721.  
  722.     if (ok)
  723.         ok = PySequence_Check(elem);
  724.     if (ok) {
  725.         PyObject *temp = PySequence_GetItem(elem, 0);
  726.         if (temp == NULL)
  727.         ok = 0;
  728.         else {
  729.         ok = PyInt_Check(temp);
  730.         if (ok)
  731.             type = PyInt_AsLong(temp);
  732.         Py_DECREF(temp);
  733.         }
  734.     }
  735.     if (!ok) {
  736.         PyErr_SetObject(parser_error,
  737.                 Py_BuildValue("(os)", elem,
  738.                       "Illegal node construct."));
  739.         Py_XDECREF(elem);
  740.         return (0);
  741.     }
  742.     if (ISTERMINAL(type)) {
  743.         if (check_terminal_tuple(elem)) {
  744.         PyObject *temp = PySequence_GetItem(elem, 1);
  745.  
  746.         strn = strdup(PyString_AsString(temp));
  747.         Py_XDECREF(temp);
  748.  
  749.         if (PyObject_Length(elem) == 3) {
  750.             PyObject* temp = PySequence_GetItem(elem, 2);
  751.             *line_num = PyInt_AsLong(temp);
  752.             Py_DECREF(temp);
  753.         }
  754.         }
  755.         else {
  756.         Py_XDECREF(elem);
  757.         return (0);
  758.         }
  759.     }
  760.     else if (!ISNONTERMINAL(type)) {
  761.         /*
  762.          *  It has to be one or the other; this is an error.
  763.          *  Throw an exception.
  764.          */
  765.         PyErr_SetObject(parser_error,
  766.                 Py_BuildValue("(os)", elem,
  767.                       "Unknown node type."));
  768.         Py_XDECREF(elem);
  769.         return (0);
  770.     }
  771.     PyNode_AddChild(root, type, strn, *line_num);
  772.  
  773.     if (ISNONTERMINAL(type)) {
  774.         node* new_child = CHILD(root, i - 1);
  775.  
  776.         if (new_child != build_node_children(elem, new_child, line_num)) {
  777.         Py_XDECREF(elem);
  778.         return (0);
  779.         }
  780.     }
  781.     else if (type == NEWLINE) {    /* It's true:  we increment the     */
  782.         ++(*line_num);        /* line number *after* the newline! */
  783.     }
  784.     Py_XDECREF(elem);
  785.     }
  786.     return (root);
  787.  
  788. }   /* build_node_children() */
  789.  
  790.  
  791. static node*
  792. build_node_tree(tuple)
  793.      PyObject *tuple;
  794. {
  795.     node* res = 0;
  796.     PyObject *temp = PySequence_GetItem(tuple, 0);
  797.     long  num = -1;
  798.  
  799.     if (temp != NULL)
  800.     num = PyInt_AsLong(temp);
  801.     Py_XDECREF(temp);
  802.     if (ISTERMINAL(num)) {
  803.     /*
  804.      *  The tuple is simple, but it doesn't start with a start symbol.
  805.      *  Throw an exception now and be done with it.
  806.      */
  807.     tuple = Py_BuildValue("(os)", tuple,
  808.             "Illegal ast tuple; cannot start with terminal symbol.");
  809.     PyErr_SetObject(parser_error, tuple);
  810.     }
  811.     else if (ISNONTERMINAL(num)) {
  812.     /*
  813.      *  Not efficient, but that can be handled later.
  814.      */
  815.     int line_num = 0;
  816.  
  817.     res = PyNode_New(num);
  818.     if (res != build_node_children(tuple, res, &line_num)) {
  819.         PyNode_Free(res);
  820.         res = 0;
  821.     }
  822.     }
  823.     else
  824.     /*  The tuple is illegal -- if the number is neither TERMINAL nor
  825.      *  NONTERMINAL, we can't use it.
  826.      */
  827.     PyErr_SetObject(parser_error,
  828.             Py_BuildValue("(os)", tuple,
  829.                       "Illegal component tuple."));
  830.  
  831.     return (res);
  832.  
  833. }   /* build_node_tree() */
  834.  
  835.  
  836. #ifdef HAVE_OLD_CPP
  837. #define VALIDATER(n)    static int validate_/**/n Py_PROTO((node *tree))
  838. #else
  839. #define    VALIDATER(n)    static int validate_##n Py_PROTO((node *tree))
  840. #endif
  841.  
  842.  
  843. /*
  844.  *  Validation routines used within the validation section:
  845.  */
  846. staticforward int validate_terminal Py_PROTO((node *terminal,
  847.                           int type, char *string));
  848.  
  849. #define    validate_ampersand(ch)    validate_terminal(ch,       AMPER, "&")
  850. #define    validate_circumflex(ch)    validate_terminal(ch, CIRCUMFLEX, "^")
  851. #define validate_colon(ch)    validate_terminal(ch,       COLON, ":")
  852. #define validate_comma(ch)    validate_terminal(ch,       COMMA, ",")
  853. #define validate_dedent(ch)    validate_terminal(ch,      DEDENT, "")
  854. #define    validate_equal(ch)    validate_terminal(ch,       EQUAL, "=")
  855. #define validate_indent(ch)    validate_terminal(ch,      INDENT, 0)
  856. #define validate_lparen(ch)    validate_terminal(ch,        LPAR, "(")
  857. #define    validate_newline(ch)    validate_terminal(ch,     NEWLINE, 0)
  858. #define validate_rparen(ch)    validate_terminal(ch,        RPAR, ")")
  859. #define validate_semi(ch)    validate_terminal(ch,        SEMI, ";")
  860. #define    validate_star(ch)    validate_terminal(ch,        STAR, "*")
  861. #define    validate_vbar(ch)    validate_terminal(ch,        VBAR, "|")
  862. #define validate_doublestar(ch)    validate_terminal(ch, DOUBLESTAR, "**")
  863. #define validate_dot(ch)    validate_terminal(ch,         DOT, ".")
  864. #define    validate_name(ch, str)    validate_terminal(ch,        NAME, str)
  865.  
  866. VALIDATER(node);        VALIDATER(small_stmt);
  867. VALIDATER(class);        VALIDATER(node);
  868. VALIDATER(parameters);        VALIDATER(suite);
  869. VALIDATER(testlist);        VALIDATER(varargslist);
  870. VALIDATER(fpdef);        VALIDATER(fplist);
  871. VALIDATER(stmt);        VALIDATER(simple_stmt);
  872. VALIDATER(expr_stmt);        VALIDATER(power);
  873. VALIDATER(print_stmt);        VALIDATER(del_stmt);
  874. VALIDATER(return_stmt);
  875. VALIDATER(raise_stmt);        VALIDATER(import_stmt);
  876. VALIDATER(global_stmt);
  877. VALIDATER(exec_stmt);        VALIDATER(compound_stmt);
  878. VALIDATER(while);        VALIDATER(for);
  879. VALIDATER(try);            VALIDATER(except_clause);
  880. VALIDATER(test);        VALIDATER(and_test);
  881. VALIDATER(not_test);        VALIDATER(comparison);
  882. VALIDATER(comp_op);        VALIDATER(expr);
  883. VALIDATER(xor_expr);        VALIDATER(and_expr);
  884. VALIDATER(shift_expr);        VALIDATER(arith_expr);
  885. VALIDATER(term);        VALIDATER(factor);
  886. VALIDATER(atom);        VALIDATER(lambdef);
  887. VALIDATER(trailer);        VALIDATER(subscript);
  888. VALIDATER(subscriptlist);    VALIDATER(sliceop);
  889. VALIDATER(exprlist);        VALIDATER(dictmaker);
  890. VALIDATER(arglist);        VALIDATER(argument);
  891.  
  892.  
  893. #define    is_even(n)    (((n) & 1) == 0)
  894. #define    is_odd(n)    (((n) & 1) == 1)
  895.  
  896.  
  897. static int
  898. validate_ntype(n, t)
  899.      node *n;
  900.      int t;
  901. {
  902.     int res = (TYPE(n) == t);
  903.  
  904.     if (!res) {
  905.     char buffer[128];
  906.     sprintf(buffer, "Expected node type %d, got %d.", t, TYPE(n));
  907.     err_string(buffer);
  908.     }
  909.     return (res);
  910.  
  911. }   /* validate_ntype() */
  912.  
  913.  
  914. static int
  915. validate_numnodes(n, num, name)
  916.      node *n;
  917.      int num;
  918.      const char *const name;
  919. {
  920.     if (NCH(n) != num) {
  921.     char buff[60];
  922.     sprintf(buff, "Illegal number of children for %s node.", name);
  923.     err_string(buff);
  924.     }
  925.     return (NCH(n) == num);
  926.  
  927. }   /* validate_numnodes() */
  928.  
  929.  
  930. static int
  931. validate_terminal(terminal, type, string)
  932.      node *terminal;
  933.      int type;
  934.      char *string;
  935. {
  936.     int res = (validate_ntype(terminal, type)
  937.            && ((string == 0) || (strcmp(string, STR(terminal)) == 0)));
  938.  
  939.     if (!res && !PyErr_Occurred()) {
  940.     char buffer[60];
  941.     sprintf(buffer, "Illegal terminal: expected \"%s\"", string);
  942.     err_string(buffer);
  943.     }
  944.     return (res);
  945.  
  946. }   /* validate_terminal() */
  947.  
  948.  
  949. /*  X (',' X) [',']
  950.  */
  951. static int
  952. validate_repeating_list(tree, ntype, vfunc, name)
  953.     node *tree;
  954.     int ntype;
  955.     int (*vfunc)();
  956.     const char *const name;
  957. {
  958.     int nch = NCH(tree);
  959.     int res = (nch && validate_ntype(tree, ntype)
  960.            && vfunc(CHILD(tree, 0)));
  961.  
  962.     if (!res && !PyErr_Occurred())
  963.     validate_numnodes(tree, 1, name);
  964.     else {
  965.     if (is_even(nch))
  966.         res = validate_comma(CHILD(tree, --nch));
  967.     if (res && nch > 1) {
  968.         int pos = 1;
  969.         for ( ; res && pos < nch; pos += 2)
  970.         res = (validate_comma(CHILD(tree, pos))
  971.                && vfunc(CHILD(tree, pos + 1)));
  972.     }
  973.     }
  974.     return (res);
  975.  
  976. }   /* validate_repeating_list() */
  977.  
  978.  
  979. /*  VALIDATE(class)
  980.  *
  981.  *  classdef:
  982.  *    'class' NAME ['(' testlist ')'] ':' suite
  983.  */
  984. static int
  985. validate_class(tree)
  986.     node *tree;
  987. {
  988.     int nch = NCH(tree);
  989.     int res = validate_ntype(tree, classdef) && ((nch == 4) || (nch == 7));
  990.  
  991.     if (res) {
  992.     res = (validate_name(CHILD(tree, 0), "class")
  993.            && validate_ntype(CHILD(tree, 1), NAME)
  994.            && validate_colon(CHILD(tree, nch - 2))
  995.            && validate_suite(CHILD(tree, nch - 1)));
  996.     }
  997.     else
  998.     validate_numnodes(tree, 4, "class");
  999.     if (res && (nch == 7)) {
  1000.     res = (validate_lparen(CHILD(tree, 2))
  1001.            && validate_testlist(CHILD(tree, 3))
  1002.            && validate_rparen(CHILD(tree, 4)));
  1003.     }
  1004.     return (res);
  1005.  
  1006. }   /* validate_class() */
  1007.  
  1008.  
  1009. /*  if_stmt:
  1010.  *    'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
  1011.  */
  1012. static int
  1013. validate_if(tree)
  1014.     node *tree;
  1015. {
  1016.     int nch = NCH(tree);
  1017.     int res = (validate_ntype(tree, if_stmt)
  1018.            && (nch >= 4)
  1019.            && validate_name(CHILD(tree, 0), "if")
  1020.            && validate_test(CHILD(tree, 1))
  1021.            && validate_colon(CHILD(tree, 2))
  1022.            && validate_suite(CHILD(tree, 3)));
  1023.  
  1024.     if (res && ((nch % 4) == 3)) {
  1025.     /*  ... 'else' ':' suite  */
  1026.     res = (validate_name(CHILD(tree, nch - 3), "else")
  1027.            && validate_colon(CHILD(tree, nch - 2))
  1028.            && validate_suite(CHILD(tree, nch - 1)));
  1029.     nch -= 3;
  1030.     }
  1031.     else if (!res && !PyErr_Occurred())
  1032.     validate_numnodes(tree, 4, "if");
  1033.     if ((nch % 4) != 0)
  1034.     /* Will catch the case for nch < 4 */
  1035.     res = validate_numnodes(tree, 0, "if");
  1036.     else if (res && (nch > 4)) {
  1037.     /*  ... ('elif' test ':' suite)+ ...  */
  1038.     int j = 4;
  1039.     while ((j < nch) && res) {
  1040.         res = (validate_name(CHILD(tree, j), "elif")
  1041.            && validate_colon(CHILD(tree, j + 2))
  1042.            && validate_test(CHILD(tree, j + 1))
  1043.            && validate_suite(CHILD(tree, j + 3)));
  1044.         j += 4;
  1045.     }
  1046.     }
  1047.     return (res);
  1048.  
  1049. }   /* validate_if() */
  1050.  
  1051.  
  1052. /*  parameters:
  1053.  *    '(' [varargslist] ')'
  1054.  *
  1055.  */
  1056. static int
  1057. validate_parameters(tree)
  1058.     node *tree;
  1059. {
  1060.     int nch = NCH(tree);
  1061.     int res = validate_ntype(tree, parameters) && ((nch == 2) || (nch == 3));
  1062.  
  1063.     if (res) {
  1064.     res = (validate_lparen(CHILD(tree, 0))
  1065.            && validate_rparen(CHILD(tree, nch - 1)));
  1066.     if (res && (nch == 3))
  1067.         res = validate_varargslist(CHILD(tree, 1));
  1068.     }
  1069.     else
  1070.     validate_numnodes(tree, 2, "parameters");
  1071.  
  1072.     return (res);
  1073.  
  1074. }   /* validate_parameters() */
  1075.  
  1076.  
  1077. /*  VALIDATE(suite)
  1078.  *
  1079.  *  suite:
  1080.  *    simple_stmt
  1081.  *    | NEWLINE INDENT stmt+ DEDENT
  1082.  */
  1083. static int
  1084. validate_suite(tree)
  1085.     node *tree;
  1086. {
  1087.     int nch = NCH(tree);
  1088.     int res = (validate_ntype(tree, suite) && ((nch == 1) || (nch >= 4)));
  1089.  
  1090.     if (res && (nch == 1))
  1091.     res = validate_simple_stmt(CHILD(tree, 0));
  1092.     else if (res) {
  1093.     /*  NEWLINE INDENT stmt+ DEDENT  */
  1094.     res = (validate_newline(CHILD(tree, 0))
  1095.            && validate_indent(CHILD(tree, 1))
  1096.            && validate_stmt(CHILD(tree, 2))
  1097.            && validate_dedent(CHILD(tree, nch - 1)));
  1098.  
  1099.     if (res && (nch > 4)) {
  1100.         int i = 3;
  1101.         --nch;            /* forget the DEDENT     */
  1102.         for ( ; res && (i < nch); ++i)
  1103.         res = validate_stmt(CHILD(tree, i));
  1104.     }
  1105.     else if (nch < 4)
  1106.         validate_numnodes(tree, 4, "suite");
  1107.     }
  1108.     return (res);
  1109.  
  1110. }   /* validate_suite() */
  1111.  
  1112.  
  1113. static int
  1114. validate_testlist(tree)
  1115.     node *tree;
  1116. {
  1117.     return (validate_repeating_list(tree, testlist,
  1118.                     validate_test, "testlist"));
  1119.  
  1120. }   /* validate_testlist() */
  1121.  
  1122.  
  1123. /*  VALIDATE(varargslist)
  1124.  *
  1125.  *  varargslist:
  1126.  *    (fpdef ['=' test] ',')* ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
  1127.  *    | fpdef ['=' test] (',' fpdef ['=' test])* [',']
  1128.  *
  1129.  *      (fpdef ['=' test] ',')*
  1130.  *           ('*' NAME [',' ('**'|'*' '*') NAME]
  1131.  *         | ('**'|'*' '*') NAME)
  1132.  *    | fpdef ['=' test] (',' fpdef ['=' test])* [',']
  1133.  *
  1134.  */
  1135. static int
  1136. validate_varargslist(tree)
  1137.     node *tree;
  1138. {
  1139.     int nch = NCH(tree);
  1140.     int res = validate_ntype(tree, varargslist) && (nch != 0);
  1141.  
  1142.     if (res && (nch >= 2) && (TYPE(CHILD(tree, nch - 1)) == NAME)) {
  1143.     /*  (fpdef ['=' test] ',')*
  1144.      *  ('*' NAME [',' '*' '*' NAME] | '*' '*' NAME)
  1145.      */
  1146.     int pos = 0;
  1147.     int remaining = nch;
  1148.  
  1149.     while (res && (TYPE(CHILD(tree, pos)) == fpdef)) {
  1150.         res = validate_fpdef(CHILD(tree, pos));
  1151.         if (res) {
  1152.         if (TYPE(CHILD(tree, pos + 1)) == EQUAL) {
  1153.             res = validate_test(CHILD(tree, pos + 2));
  1154.             pos += 2;
  1155.         }
  1156.         res = res && validate_comma(CHILD(tree, pos + 1));
  1157.         pos += 2;
  1158.         }
  1159.     }
  1160.     if (res) {
  1161.         remaining = nch - pos;
  1162.         res = ((remaining == 2) || (remaining == 3)
  1163.            || (remaining == 5) || (remaining == 6));
  1164.         if (!res)
  1165.         validate_numnodes(tree, 2, "varargslist");
  1166.         else if (TYPE(CHILD(tree, pos)) == DOUBLESTAR)
  1167.         return ((remaining == 2)
  1168.             && validate_ntype(CHILD(tree, pos+1), NAME));
  1169.         else {
  1170.         res = validate_star(CHILD(tree, pos++));
  1171.         --remaining;
  1172.         }
  1173.     }
  1174.     if (res) {
  1175.         if (remaining == 2) {
  1176.         res = (validate_star(CHILD(tree, pos))
  1177.                && validate_ntype(CHILD(tree, pos + 1), NAME));
  1178.         }
  1179.         else {
  1180.         res = validate_ntype(CHILD(tree, pos++), NAME);
  1181.         if (res && (remaining >= 4)) {
  1182.             res = validate_comma(CHILD(tree, pos));
  1183.             if (--remaining == 3)
  1184.             res = (validate_star(CHILD(tree, pos + 1))
  1185.                    && validate_star(CHILD(tree, pos + 2)));
  1186.             else
  1187.             validate_ntype(CHILD(tree, pos + 1), DOUBLESTAR);
  1188.         }
  1189.         }
  1190.     }
  1191.     if (!res && !PyErr_Occurred())
  1192.         err_string("Incorrect validation of variable arguments list.");
  1193.     }
  1194.     else if (res) {
  1195.     /*  fpdef ['=' test] (',' fpdef ['=' test])* [',']  */
  1196.     if (TYPE(CHILD(tree, nch - 1)) == COMMA)
  1197.         --nch;
  1198.  
  1199.     /*  fpdef ['=' test] (',' fpdef ['=' test])*  */
  1200.     res = (is_odd(nch)
  1201.            && validate_fpdef(CHILD(tree, 0)));
  1202.  
  1203.     if (res && (nch > 1)) {
  1204.         int pos = 1;
  1205.         if (TYPE(CHILD(tree, 1)) == EQUAL) {
  1206.         res = validate_test(CHILD(tree, 2));
  1207.         pos += 2;
  1208.         }
  1209.         /*  ... (',' fpdef ['=' test])*  */
  1210.         for ( ; res && (pos < nch); pos += 2) {
  1211.         /* ',' fpdef */
  1212.         res = (validate_comma(CHILD(tree, pos))
  1213.                && validate_fpdef(CHILD(tree, pos + 1)));
  1214.         if (res
  1215.             && ((nch - pos) > 2)
  1216.             && (TYPE(CHILD(tree, pos + 2)) == EQUAL)) {
  1217.             /* ['=' test] */
  1218.             res = validate_test(CHILD(tree, pos + 3));
  1219.             pos += 2;
  1220.         }
  1221.         }
  1222.     }
  1223.     }
  1224.     else
  1225.     err_string("Improperly formed argument list.");
  1226.  
  1227.     return (res);
  1228.  
  1229. }   /* validate_varargslist() */
  1230.  
  1231.  
  1232. /*  VALIDATE(fpdef)
  1233.  *
  1234.  *  fpdef:
  1235.  *    NAME
  1236.  *    | '(' fplist ')'
  1237.  */
  1238. static int
  1239. validate_fpdef(tree)
  1240.     node *tree;
  1241. {
  1242.     int nch = NCH(tree);
  1243.     int res = validate_ntype(tree, fpdef);
  1244.  
  1245.     if (res) {
  1246.     if (nch == 1)
  1247.         res = validate_ntype(CHILD(tree, 0), NAME);
  1248.     else if (nch == 3)
  1249.         res = (validate_lparen(CHILD(tree, 0))
  1250.            && validate_fplist(CHILD(tree, 1))
  1251.            && validate_rparen(CHILD(tree, 2)));
  1252.     else
  1253.         validate_numnodes(tree, 1, "fpdef");
  1254.     }
  1255.     return (res);
  1256.  
  1257. } /* validate_fpdef() */
  1258.  
  1259.  
  1260. static int
  1261. validate_fplist(tree)
  1262.     node *tree;
  1263. {
  1264.     return (validate_repeating_list(tree, fplist,
  1265.                     validate_fpdef, "fplist"));
  1266.  
  1267. }   /* validate_fplist() */
  1268.  
  1269.  
  1270. /*  simple_stmt | compound_stmt
  1271.  *
  1272.  */
  1273. static int
  1274. validate_stmt(tree)
  1275.     node *tree;
  1276. {
  1277.     int res = (validate_ntype(tree, stmt)
  1278.            && validate_numnodes(tree, 1, "stmt"));
  1279.  
  1280.     if (res) {
  1281.     tree = CHILD(tree, 0);
  1282.  
  1283.     if (TYPE(tree) == simple_stmt)
  1284.         res = validate_simple_stmt(tree);
  1285.     else
  1286.         res = validate_compound_stmt(tree);
  1287.     }
  1288.     return (res);
  1289.  
  1290. }   /* validate_stmt() */
  1291.  
  1292.  
  1293. /*  small_stmt (';' small_stmt)* [';'] NEWLINE
  1294.  *
  1295.  */
  1296. static int
  1297. validate_simple_stmt(tree)
  1298.     node *tree;
  1299. {
  1300.     int nch = NCH(tree);
  1301.     int res = (validate_ntype(tree, simple_stmt)
  1302.            && (nch >= 2)
  1303.            && validate_small_stmt(CHILD(tree, 0))
  1304.            && validate_newline(CHILD(tree, nch - 1)));
  1305.  
  1306.     if (nch < 2)
  1307.     res = validate_numnodes(tree, 2, "simple_stmt");
  1308.     --nch;                /* forget the NEWLINE     */
  1309.     if (res && is_even(nch))
  1310.     res = validate_semi(CHILD(tree, --nch));
  1311.     if (res && (nch > 2)) {
  1312.     int i;
  1313.  
  1314.     for (i = 1; res && (i < nch); i += 2)
  1315.         res = (validate_semi(CHILD(tree, i))
  1316.            && validate_small_stmt(CHILD(tree, i + 1)));
  1317.     }
  1318.     return (res);
  1319.  
  1320. }   /* validate_simple_stmt() */
  1321.  
  1322.  
  1323. static int
  1324. validate_small_stmt(tree)
  1325.     node *tree;
  1326. {
  1327.     int nch = NCH(tree);
  1328.     int    res = (validate_numnodes(tree, 1, "small_stmt")
  1329.            && ((TYPE(CHILD(tree, 0)) == expr_stmt)
  1330.            || (TYPE(CHILD(tree, 0)) == print_stmt)
  1331.            || (TYPE(CHILD(tree, 0)) == del_stmt)
  1332.            || (TYPE(CHILD(tree, 0)) == pass_stmt)
  1333.            || (TYPE(CHILD(tree, 0)) == flow_stmt)
  1334.            || (TYPE(CHILD(tree, 0)) == import_stmt)
  1335.            || (TYPE(CHILD(tree, 0)) == global_stmt)
  1336.            || (TYPE(CHILD(tree, 0)) == exec_stmt)));
  1337.  
  1338.     if (res)
  1339.     res = validate_node(CHILD(tree, 0));
  1340.     else if (nch == 1) {
  1341.     char buffer[60];
  1342.     sprintf(buffer, "Unrecognized child node of small_stmt: %d.",
  1343.         TYPE(CHILD(tree, 0)));
  1344.     err_string(buffer);
  1345.     }
  1346.     return (res);
  1347.  
  1348. }   /* validate_small_stmt */
  1349.  
  1350.  
  1351. /*  compound_stmt:
  1352.  *    if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
  1353.  */
  1354. static int
  1355. validate_compound_stmt(tree)
  1356.     node *tree;
  1357. {
  1358.     int res = (validate_ntype(tree, compound_stmt)
  1359.            && validate_numnodes(tree, 1, "compound_stmt"));
  1360.  
  1361.     if (!res)
  1362.     return (0);
  1363.  
  1364.     tree = CHILD(tree, 0);
  1365.     res = ((TYPE(tree) == if_stmt)
  1366.        || (TYPE(tree) == while_stmt)
  1367.        || (TYPE(tree) == for_stmt)
  1368.        || (TYPE(tree) == try_stmt)
  1369.        || (TYPE(tree) == funcdef)
  1370.        || (TYPE(tree) == classdef));
  1371.     if (res)
  1372.     res = validate_node(tree);
  1373.     else {
  1374.     char buffer[60];
  1375.     sprintf(buffer, "Illegal compound statement type: %d.", TYPE(tree));
  1376.     err_string(buffer);
  1377.     }
  1378.     return (res);
  1379.  
  1380. }   /* validate_compound_stmt() */
  1381.  
  1382.  
  1383. static int
  1384. validate_expr_stmt(tree)
  1385.     node *tree;
  1386. {
  1387.     int j;
  1388.     int nch = NCH(tree);
  1389.     int res = (validate_ntype(tree, expr_stmt)
  1390.            && is_odd(nch)
  1391.            && validate_testlist(CHILD(tree, 0)));
  1392.  
  1393.     for (j = 1; res && (j < nch); j += 2)
  1394.     res = (validate_equal(CHILD(tree, j))
  1395.            && validate_testlist(CHILD(tree, j + 1)));
  1396.  
  1397.     return (res);
  1398.  
  1399. }   /* validate_expr_stmt() */
  1400.  
  1401.  
  1402. /*  print_stmt:
  1403.  *
  1404.  *    'print' (test ',')* [test]
  1405.  *
  1406.  */
  1407. static int
  1408. validate_print_stmt(tree)
  1409.     node *tree;
  1410. {
  1411.     int j;
  1412.     int nch = NCH(tree);
  1413.     int res = (validate_ntype(tree, print_stmt)
  1414.            && (nch != 0)
  1415.            && validate_name(CHILD(tree, 0), "print"));
  1416.  
  1417.     if (res && is_even(nch)) {
  1418.     res = validate_test(CHILD(tree, nch - 1));
  1419.     --nch;
  1420.     }
  1421.     else if (!res && !PyErr_Occurred())
  1422.     validate_numnodes(tree, 1, "print_stmt");
  1423.     for (j = 1; res && (j < nch); j += 2)
  1424.     res = (validate_test(CHILD(tree, j))
  1425.            && validate_ntype(CHILD(tree, j + 1), COMMA));
  1426.  
  1427.     return (res);
  1428.  
  1429. }   /* validate_print_stmt() */
  1430.  
  1431.  
  1432. static int
  1433. validate_del_stmt(tree)
  1434.     node *tree;
  1435. {
  1436.     return (validate_numnodes(tree, 2, "del_stmt")
  1437.         && validate_name(CHILD(tree, 0), "del")
  1438.         && validate_exprlist(CHILD(tree, 1)));
  1439.  
  1440. }   /* validate_del_stmt() */
  1441.  
  1442.  
  1443. static int
  1444. validate_return_stmt(tree)
  1445.     node *tree;
  1446. {
  1447.     int nch = NCH(tree);
  1448.     int res = (validate_ntype(tree, return_stmt)
  1449.            && ((nch == 1) || (nch == 2))
  1450.            && validate_name(CHILD(tree, 0), "return"));
  1451.  
  1452.     if (res && (nch == 2))
  1453.     res = validate_testlist(CHILD(tree, 1));
  1454.  
  1455.     return (res);
  1456.  
  1457. }   /* validate_return_stmt() */
  1458.  
  1459.  
  1460. static int
  1461. validate_raise_stmt(tree)
  1462.     node *tree;
  1463. {
  1464.     int nch = NCH(tree);
  1465.     int res = (validate_ntype(tree, raise_stmt)
  1466.            && ((nch == 2) || (nch == 4) || (nch == 6)));
  1467.  
  1468.     if (res) {
  1469.     res = (validate_name(CHILD(tree, 0), "raise")
  1470.            && validate_test(CHILD(tree, 1)));
  1471.     if (res && nch > 2) {
  1472.         res = (validate_comma(CHILD(tree, 2))
  1473.            && validate_test(CHILD(tree, 3)));
  1474.         if (res && (nch > 4))
  1475.         res = (validate_comma(CHILD(tree, 4))
  1476.                && validate_test(CHILD(tree, 5)));
  1477.     }
  1478.     }
  1479.     else
  1480.     validate_numnodes(tree, 2, "raise");
  1481.     if (res && (nch == 4))
  1482.     res = (validate_comma(CHILD(tree, 2))
  1483.            && validate_test(CHILD(tree, 3)));
  1484.  
  1485.     return (res);
  1486.  
  1487. }   /* validate_raise_stmt() */
  1488.  
  1489.  
  1490. /*  import_stmt:
  1491.  *
  1492.  *    'import' dotted_name (',' dotted_name)*
  1493.  *  | 'from' dotted_name 'import' ('*' | NAME (',' NAME)*)
  1494.  */
  1495. static int
  1496. validate_import_stmt(tree)
  1497.     node *tree;
  1498. {
  1499.     int nch = NCH(tree);
  1500.     int res = (validate_ntype(tree, import_stmt)
  1501.            && (nch >= 2) && is_even(nch)
  1502.            && validate_ntype(CHILD(tree, 0), NAME)
  1503.            && validate_ntype(CHILD(tree, 1), dotted_name));
  1504.  
  1505.     if (res && (strcmp(STR(CHILD(tree, 0)), "import") == 0)) {
  1506.     int j;
  1507.  
  1508.     for (j = 2; res && (j < nch); j += 2)
  1509.         res = (validate_comma(CHILD(tree, j))
  1510.            && validate_ntype(CHILD(tree, j + 1), dotted_name));
  1511.     }
  1512.     else if (res && validate_name(CHILD(tree, 0), "from")) {
  1513.     res = ((nch >= 4) && is_even(nch)
  1514.            && validate_name(CHILD(tree, 2), "import"));
  1515.     if (nch == 4) {
  1516.         res = ((TYPE(CHILD(tree, 3)) == NAME)
  1517.            || (TYPE(CHILD(tree, 3)) == STAR));
  1518.         if (!res)
  1519.         err_string("Illegal import statement.");
  1520.     }
  1521.     else {
  1522.         /*  'from' NAME 'import' NAME (',' NAME)+  */
  1523.         int j;
  1524.         res = validate_ntype(CHILD(tree, 3), NAME);
  1525.         for (j = 4; res && (j < nch); j += 2)
  1526.         res = (validate_comma(CHILD(tree, j))
  1527.                && validate_ntype(CHILD(tree, j + 1), NAME));
  1528.     }
  1529.     }
  1530.     else
  1531.     res = 0;
  1532.  
  1533.     return (res);
  1534.  
  1535. }   /* validate_import_stmt() */
  1536.  
  1537.  
  1538. static int
  1539. validate_global_stmt(tree)
  1540.     node *tree;
  1541. {
  1542.     int j;
  1543.     int nch = NCH(tree);
  1544.     int res = (validate_ntype(tree, global_stmt)
  1545.            && is_even(nch) && (nch >= 2));
  1546.  
  1547.     if (res)
  1548.     res = (validate_name(CHILD(tree, 0), "global")
  1549.            && validate_ntype(CHILD(tree, 1), NAME));
  1550.     for (j = 2; res && (j < nch); j += 2)
  1551.     res = (validate_comma(CHILD(tree, j))
  1552.            && validate_ntype(CHILD(tree, j + 1), NAME));
  1553.  
  1554.     return (res);
  1555.  
  1556. }   /* validate_global_stmt() */
  1557.  
  1558.  
  1559. /*  exec_stmt:
  1560.  *
  1561.  *  'exec' expr ['in' test [',' test]]
  1562.  */
  1563. static int
  1564. validate_exec_stmt(tree)
  1565.     node *tree;
  1566. {
  1567.     int nch = NCH(tree);
  1568.     int res = (validate_ntype(tree, exec_stmt)
  1569.            && ((nch == 2) || (nch == 4) || (nch == 6))
  1570.            && validate_name(CHILD(tree, 0), "exec")
  1571.            && validate_expr(CHILD(tree, 1)));
  1572.  
  1573.     if (!res && !PyErr_Occurred())
  1574.     err_string("Illegal exec statement.");
  1575.     if (res && (nch > 2))
  1576.     res = (validate_name(CHILD(tree, 2), "in")
  1577.            && validate_test(CHILD(tree, 3)));
  1578.     if (res && (nch == 6))
  1579.     res = (validate_comma(CHILD(tree, 4))
  1580.            && validate_test(CHILD(tree, 5)));
  1581.  
  1582.     return (res);
  1583.  
  1584. }   /* validate_exec_stmt() */
  1585.  
  1586.  
  1587. static int
  1588. validate_while(tree)
  1589.     node *tree;
  1590. {
  1591.     int nch = NCH(tree);
  1592.     int res = (validate_ntype(tree, while_stmt)
  1593.            && ((nch == 4) || (nch == 7))
  1594.            && validate_name(CHILD(tree, 0), "while")
  1595.            && validate_test(CHILD(tree, 1))
  1596.            && validate_colon(CHILD(tree, 2))
  1597.            && validate_suite(CHILD(tree, 3)));
  1598.  
  1599.     if (res && (nch == 7))
  1600.     res = (validate_name(CHILD(tree, 4), "else")
  1601.            && validate_colon(CHILD(tree, 5))
  1602.            && validate_suite(CHILD(tree, 6)));
  1603.  
  1604.     return (res);
  1605.  
  1606. }   /* validate_while() */
  1607.  
  1608.  
  1609. static int
  1610. validate_for(tree)
  1611.     node *tree;
  1612. {
  1613.     int nch = NCH(tree);
  1614.     int res = (validate_ntype(tree, for_stmt)
  1615.            && ((nch == 6) || (nch == 9))
  1616.            && validate_name(CHILD(tree, 0), "for")
  1617.            && validate_exprlist(CHILD(tree, 1))
  1618.            && validate_name(CHILD(tree, 2), "in")
  1619.            && validate_testlist(CHILD(tree, 3))
  1620.            && validate_colon(CHILD(tree, 4))
  1621.            && validate_suite(CHILD(tree, 5)));
  1622.  
  1623.     if (res && (nch == 9))
  1624.     res = (validate_name(CHILD(tree, 6), "else")
  1625.            && validate_colon(CHILD(tree, 7))
  1626.            && validate_suite(CHILD(tree, 8)));
  1627.  
  1628.     return (res);
  1629.  
  1630. }   /* validate_for() */
  1631.  
  1632.  
  1633. /*  try_stmt:
  1634.  *    'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
  1635.  *    | 'try' ':' suite 'finally' ':' suite
  1636.  *
  1637.  */
  1638. static int
  1639. validate_try(tree)
  1640.     node *tree;
  1641. {
  1642.     int nch = NCH(tree);
  1643.     int pos = 3;
  1644.     int res = (validate_ntype(tree, try_stmt)
  1645.            && (nch >= 6) && ((nch % 3) == 0));
  1646.  
  1647.     if (res)
  1648.     res = (validate_name(CHILD(tree, 0), "try")
  1649.            && validate_colon(CHILD(tree, 1))
  1650.            && validate_suite(CHILD(tree, 2))
  1651.            && validate_colon(CHILD(tree, nch - 2))
  1652.            && validate_suite(CHILD(tree, nch - 1)));
  1653.     else {
  1654.     const char* name = "execpt";
  1655.     char buffer[60];
  1656.     if (TYPE(CHILD(tree, nch - 3)) != except_clause)
  1657.         name = STR(CHILD(tree, nch - 3));
  1658.     sprintf(buffer, "Illegal number of children for try/%s node.", name);
  1659.     err_string(buffer);
  1660.     }
  1661.     /*    Skip past except_clause sections:  */
  1662.     while (res && (TYPE(CHILD(tree, pos)) == except_clause)) {
  1663.     res = (validate_except_clause(CHILD(tree, pos))
  1664.            && validate_colon(CHILD(tree, pos + 1))
  1665.            && validate_suite(CHILD(tree, pos + 2)));
  1666.     pos += 3;
  1667.     }
  1668.     if (res && (pos < nch)) {
  1669.     res = validate_ntype(CHILD(tree, pos), NAME);
  1670.     if (res && (strcmp(STR(CHILD(tree, pos)), "finally") == 0))
  1671.         res = (validate_numnodes(tree, 6, "try/finally")
  1672.            && validate_colon(CHILD(tree, 4))
  1673.            && validate_suite(CHILD(tree, 5)));
  1674.     else if (res)
  1675.         if (nch == (pos + 3)) {
  1676.         res = ((strcmp(STR(CHILD(tree, pos)), "except") == 0)
  1677.                || (strcmp(STR(CHILD(tree, pos)), "else") == 0));
  1678.         if (!res)
  1679.             err_string("Illegal trailing triple in try statement.");
  1680.         }
  1681.         else if (nch == (pos + 6))
  1682.         res = (validate_name(CHILD(tree, pos), "except")
  1683.                && validate_colon(CHILD(tree, pos + 1))
  1684.                && validate_suite(CHILD(tree, pos + 2))
  1685.                && validate_name(CHILD(tree, pos + 3), "else"));
  1686.         else
  1687.         res = validate_numnodes(tree, pos + 3, "try/except");
  1688.     }
  1689.     return (res);
  1690.  
  1691. }   /* validate_try() */
  1692.  
  1693.  
  1694. static int
  1695. validate_except_clause(tree)
  1696.     node *tree;
  1697. {
  1698.     int nch = NCH(tree);
  1699.     int res = (validate_ntype(tree, except_clause)
  1700.            && ((nch == 1) || (nch == 2) || (nch == 4))
  1701.            && validate_name(CHILD(tree, 0), "except"));
  1702.  
  1703.     if (res && (nch > 1))
  1704.     res = validate_test(CHILD(tree, 1));
  1705.     if (res && (nch == 4))
  1706.     res = (validate_comma(CHILD(tree, 2))
  1707.            && validate_test(CHILD(tree, 3)));
  1708.  
  1709.     return (res);
  1710.  
  1711. }   /* validate_except_clause() */
  1712.  
  1713.  
  1714. static int
  1715. validate_test(tree)
  1716.     node *tree;
  1717. {
  1718.     int nch = NCH(tree);
  1719.     int res = validate_ntype(tree, test) && is_odd(nch);
  1720.  
  1721.     if (res && (TYPE(CHILD(tree, 0)) == lambdef))
  1722.     res = ((nch == 1)
  1723.            && validate_lambdef(CHILD(tree, 0)));
  1724.     else if (res) {
  1725.     int pos;
  1726.     res = validate_and_test(CHILD(tree, 0));
  1727.     for (pos = 1; res && (pos < nch); pos += 2)
  1728.         res = (validate_name(CHILD(tree, pos), "or")
  1729.            && validate_and_test(CHILD(tree, pos + 1)));
  1730.     }
  1731.     return (res);
  1732.  
  1733. }   /* validate_test() */
  1734.  
  1735.  
  1736. static int
  1737. validate_and_test(tree)
  1738.     node *tree;
  1739. {
  1740.     int pos;
  1741.     int nch = NCH(tree);
  1742.     int res = (validate_ntype(tree, and_test)
  1743.            && is_odd(nch)
  1744.            && validate_not_test(CHILD(tree, 0)));
  1745.  
  1746.     for (pos = 1; res && (pos < nch); pos += 2)
  1747.     res = (validate_name(CHILD(tree, pos), "and")
  1748.            && validate_not_test(CHILD(tree, 0)));
  1749.  
  1750.     return (res);
  1751.  
  1752. }   /* validate_and_test() */
  1753.  
  1754.  
  1755. static int
  1756. validate_not_test(tree)
  1757.     node *tree;
  1758. {
  1759.     int nch = NCH(tree);
  1760.     int res = validate_ntype(tree, not_test) && ((nch == 1) || (nch == 2));
  1761.  
  1762.     if (res) {
  1763.     if (nch == 2)
  1764.         res = (validate_name(CHILD(tree, 0), "not")
  1765.            && validate_not_test(CHILD(tree, 1)));
  1766.     else if (nch == 1)
  1767.         res = validate_comparison(CHILD(tree, 0));
  1768.     }
  1769.     return (res);
  1770.  
  1771. }   /* validate_not_test() */
  1772.  
  1773.  
  1774. static int
  1775. validate_comparison(tree)
  1776.     node *tree;
  1777. {
  1778.     int pos;
  1779.     int nch = NCH(tree);
  1780.     int res = (validate_ntype(tree, comparison)
  1781.            && is_odd(nch)
  1782.            && validate_expr(CHILD(tree, 0)));
  1783.  
  1784.     for (pos = 1; res && (pos < nch); pos += 2)
  1785.     res = (validate_comp_op(CHILD(tree, pos))
  1786.            && validate_expr(CHILD(tree, pos + 1)));
  1787.  
  1788.     return (res);
  1789.  
  1790. }   /* validate_comparison() */
  1791.  
  1792.  
  1793. static int
  1794. validate_comp_op(tree)
  1795.     node *tree;
  1796. {
  1797.     int res = 0;
  1798.     int nch = NCH(tree);
  1799.  
  1800.     if (!validate_ntype(tree, comp_op))
  1801.     return (0);
  1802.     if (nch == 1) {
  1803.     /*
  1804.      *  Only child will be a terminal with a well-defined symbolic name
  1805.      *  or a NAME with a string of either 'is' or 'in'
  1806.      */
  1807.     tree = CHILD(tree, 0);
  1808.     switch (TYPE(tree)) {
  1809.         case LESS:
  1810.         case GREATER:
  1811.         case EQEQUAL:
  1812.         case EQUAL:
  1813.         case LESSEQUAL:
  1814.         case GREATEREQUAL:
  1815.         case NOTEQUAL:
  1816.           res = 1;
  1817.           break;
  1818.         case NAME:
  1819.           res = ((strcmp(STR(tree), "in") == 0)
  1820.              || (strcmp(STR(tree), "is") == 0));
  1821.           if (!res) {
  1822.           char buff[128];
  1823.           sprintf(buff, "Illegal operator: '%s'.", STR(tree));
  1824.           err_string(buff);
  1825.           }
  1826.           break;
  1827.       default:
  1828.           err_string("Illegal comparison operator type.");
  1829.           break;
  1830.     }
  1831.     }
  1832.     else if (res = validate_numnodes(tree, 2, "comp_op")) {
  1833.     res = (validate_ntype(CHILD(tree, 0), NAME)
  1834.            && validate_ntype(CHILD(tree, 1), NAME)
  1835.            && (((strcmp(STR(CHILD(tree, 0)), "is") == 0)
  1836.             && (strcmp(STR(CHILD(tree, 1)), "not") == 0))
  1837.            || ((strcmp(STR(CHILD(tree, 0)), "not") == 0)
  1838.                && (strcmp(STR(CHILD(tree, 1)), "in") == 0))));
  1839.     if (!res && !PyErr_Occurred())
  1840.         err_string("Unknown comparison operator.");
  1841.     }
  1842.     return (res);
  1843.  
  1844. }   /* validate_comp_op() */
  1845.  
  1846.  
  1847. static int
  1848. validate_expr(tree)
  1849.     node *tree;
  1850. {
  1851.     int j;
  1852.     int nch = NCH(tree);
  1853.     int res = (validate_ntype(tree, expr)
  1854.            && is_odd(nch)
  1855.            && validate_xor_expr(CHILD(tree, 0)));
  1856.  
  1857.     for (j = 2; res && (j < nch); j += 2)
  1858.     res = (validate_xor_expr(CHILD(tree, j))
  1859.            && validate_vbar(CHILD(tree, j - 1)));
  1860.  
  1861.     return (res);
  1862.  
  1863. }   /* validate_expr() */
  1864.  
  1865.  
  1866. static int
  1867. validate_xor_expr(tree)
  1868.     node *tree;
  1869. {
  1870.     int j;
  1871.     int nch = NCH(tree);
  1872.     int res = (validate_ntype(tree, xor_expr)
  1873.            && is_odd(nch)
  1874.            && validate_and_expr(CHILD(tree, 0)));
  1875.  
  1876.     for (j = 2; res && (j < nch); j += 2)
  1877.     res = (validate_circumflex(CHILD(tree, j - 1))
  1878.            && validate_and_expr(CHILD(tree, j)));
  1879.  
  1880.     return (res);
  1881.  
  1882. }   /* validate_xor_expr() */
  1883.  
  1884.  
  1885. static int
  1886. validate_and_expr(tree)
  1887.     node *tree;
  1888. {
  1889.     int pos;
  1890.     int nch = NCH(tree);
  1891.     int res = (validate_ntype(tree, and_expr)
  1892.            && is_odd(nch)
  1893.            && validate_shift_expr(CHILD(tree, 0)));
  1894.  
  1895.     for (pos = 1; res && (pos < nch); pos += 2)
  1896.     res = (validate_ampersand(CHILD(tree, pos))
  1897.            && validate_shift_expr(CHILD(tree, pos + 1)));
  1898.  
  1899.     return (res);
  1900.  
  1901. }   /* validate_and_expr() */
  1902.  
  1903.  
  1904. static int
  1905. validate_chain_two_ops(tree, termvalid, op1, op2)
  1906.      node *tree;
  1907.      int (*termvalid)();
  1908.      int op1;
  1909.      int op2;
  1910.  {
  1911.     int pos = 1;
  1912.     int nch = NCH(tree);
  1913.     int res = (is_odd(nch)
  1914.            && (*termvalid)(CHILD(tree, 0)));
  1915.  
  1916.     for ( ; res && (pos < nch); pos += 2) {
  1917.     if (TYPE(CHILD(tree, pos)) != op1)
  1918.         res = validate_ntype(CHILD(tree, pos), op2);
  1919.     if (res)
  1920.         res = (*termvalid)(CHILD(tree, pos + 1));
  1921.     }
  1922.     return (res);
  1923.  
  1924. }   /* validate_chain_two_ops() */
  1925.  
  1926.  
  1927. static int
  1928. validate_shift_expr(tree)
  1929.     node *tree;
  1930. {
  1931.     return (validate_ntype(tree, shift_expr)
  1932.         && validate_chain_two_ops(tree, validate_arith_expr,
  1933.                       LEFTSHIFT, RIGHTSHIFT));
  1934.  
  1935. }   /* validate_shift_expr() */
  1936.  
  1937.  
  1938. static int
  1939. validate_arith_expr(tree)
  1940.     node *tree;
  1941. {
  1942.     return (validate_ntype(tree, arith_expr)
  1943.         && validate_chain_two_ops(tree, validate_term, PLUS, MINUS));
  1944.  
  1945. }   /* validate_arith_expr() */
  1946.  
  1947.  
  1948. static int
  1949. validate_term(tree)
  1950.     node *tree;
  1951. {
  1952.     int pos = 1;
  1953.     int nch = NCH(tree);
  1954.     int res = (validate_ntype(tree, term)
  1955.            && is_odd(nch)
  1956.            && validate_factor(CHILD(tree, 0)));
  1957.  
  1958.     for ( ; res && (pos < nch); pos += 2)
  1959.     res = (((TYPE(CHILD(tree, pos)) == STAR)
  1960.            || (TYPE(CHILD(tree, pos)) == SLASH)
  1961.            || (TYPE(CHILD(tree, pos)) == PERCENT))
  1962.            && validate_factor(CHILD(tree, pos + 1)));
  1963.  
  1964.     return (res);
  1965.  
  1966. }   /* validate_term() */
  1967.  
  1968.  
  1969. /*  factor:
  1970.  *
  1971.  *  factor: ('+'|'-'|'~') factor | power
  1972.  */
  1973. static int
  1974. validate_factor(tree)
  1975.     node *tree;
  1976. {
  1977.     int nch = NCH(tree);
  1978.     int res = (validate_ntype(tree, factor)
  1979.            && (((nch == 2)
  1980.             && ((TYPE(CHILD(tree, 0)) == PLUS)
  1981.                 || (TYPE(CHILD(tree, 0)) == MINUS)
  1982.                 || (TYPE(CHILD(tree, 0)) == TILDE))
  1983.             && validate_factor(CHILD(tree, 1)))
  1984.            || ((nch == 1)
  1985.                && validate_power(CHILD(tree, 0)))));
  1986.     return (res);
  1987.  
  1988. }   /* validate_factor() */
  1989.  
  1990.  
  1991. /*  power:
  1992.  *
  1993.  *  power: atom trailer* ('**' factor)*
  1994.  */
  1995. static int
  1996. validate_power(tree)
  1997.     node *tree;
  1998. {
  1999.     int pos = 1;
  2000.     int nch = NCH(tree);
  2001.     int res = (validate_ntype(tree, power) && (nch >= 1)
  2002.            && validate_atom(CHILD(tree, 0)));
  2003.  
  2004.     while (res && (pos < nch) && (TYPE(CHILD(tree, pos)) == trailer))
  2005.     res = validate_trailer(CHILD(tree, pos++));
  2006.     if (res && (pos < nch)) {
  2007.     if (!is_even(nch - pos)) {
  2008.         err_string("Illegal number of nodes for 'power'.");
  2009.         return (0);
  2010.     }
  2011.     for ( ; res && (pos < (nch - 1)); pos += 2)
  2012.         res = (validate_doublestar(CHILD(tree, pos))
  2013.            && validate_factor(CHILD(tree, pos + 1)));
  2014.     }
  2015.     return (res);
  2016.  
  2017. }   /* validate_power() */
  2018.  
  2019.  
  2020. static int
  2021. validate_atom(tree)
  2022.     node *tree;
  2023. {
  2024.     int pos;
  2025.     int nch = NCH(tree);
  2026.     int res = validate_ntype(tree, atom) && (nch >= 1);
  2027.  
  2028.     if (res) {
  2029.     switch (TYPE(CHILD(tree, 0))) {
  2030.       case LPAR:
  2031.         res = ((nch <= 3)
  2032.            && (validate_rparen(CHILD(tree, nch - 1))));
  2033.  
  2034.         if (res && (nch == 3))
  2035.         res = validate_testlist(CHILD(tree, 1));
  2036.         break;
  2037.       case LSQB:
  2038.         res = ((nch <= 3)
  2039.            && validate_ntype(CHILD(tree, nch - 1), RSQB));
  2040.  
  2041.         if (res && (nch == 3))
  2042.         res = validate_testlist(CHILD(tree, 1));
  2043.         break;
  2044.       case LBRACE:
  2045.         res = ((nch <= 3)
  2046.            && validate_ntype(CHILD(tree, nch - 1), RBRACE));
  2047.  
  2048.         if (res && (nch == 3))
  2049.         res = validate_dictmaker(CHILD(tree, 1));
  2050.         break;
  2051.       case BACKQUOTE:
  2052.         res = ((nch == 3)
  2053.            && validate_testlist(CHILD(tree, 1))
  2054.            && validate_ntype(CHILD(tree, 2), BACKQUOTE));
  2055.         break;
  2056.       case NAME:
  2057.       case NUMBER:
  2058.         res = (nch == 1);
  2059.         break;
  2060.       case STRING:
  2061.         for (pos = 1; res && (pos < nch); ++pos)
  2062.         res = validate_ntype(CHILD(tree, pos), STRING);
  2063.         break;
  2064.       default:
  2065.         res = 0;
  2066.         break;
  2067.     }
  2068.     }
  2069.     return (res);
  2070.  
  2071. }   /* validate_atom() */
  2072.  
  2073.  
  2074. /*  funcdef:
  2075.  *    'def' NAME parameters ':' suite
  2076.  *
  2077.  */
  2078. static int
  2079. validate_funcdef(tree)
  2080.     node *tree;
  2081. {
  2082.     return (validate_ntype(tree, funcdef)
  2083.         && validate_numnodes(tree, 5, "funcdef")
  2084.         && validate_name(CHILD(tree, 0), "def")
  2085.         && validate_ntype(CHILD(tree, 1), NAME)
  2086.         && validate_colon(CHILD(tree, 3))
  2087.         && validate_parameters(CHILD(tree, 2))
  2088.         && validate_suite(CHILD(tree, 4)));
  2089.  
  2090. }   /* validate_funcdef() */
  2091.  
  2092.  
  2093. static int
  2094. validate_lambdef(tree)
  2095.     node *tree;
  2096. {
  2097.     int nch = NCH(tree);
  2098.     int res = (validate_ntype(tree, lambdef)
  2099.            && ((nch == 3) || (nch == 4))
  2100.            && validate_name(CHILD(tree, 0), "lambda")
  2101.            && validate_colon(CHILD(tree, nch - 2))
  2102.            && validate_test(CHILD(tree, nch - 1)));
  2103.  
  2104.     if (res && (nch == 4))
  2105.     res = validate_varargslist(CHILD(tree, 1));
  2106.     else if (!res && !PyErr_Occurred())
  2107.     validate_numnodes(tree, 3, "lambdef");
  2108.  
  2109.     return (res);
  2110.  
  2111. }   /* validate_lambdef() */
  2112.  
  2113.  
  2114. /*  arglist:
  2115.  *
  2116.  *  argument (',' argument)* [',']
  2117.  */
  2118. static int
  2119. validate_arglist(tree)
  2120.     node *tree;
  2121. {
  2122.     return (validate_repeating_list(tree, arglist,
  2123.                     validate_argument, "arglist"));
  2124.  
  2125. }   /* validate_arglist() */
  2126.  
  2127.  
  2128.  
  2129. /*  argument:
  2130.  *
  2131.  *  [test '='] test
  2132.  */
  2133. static int
  2134. validate_argument(tree)
  2135.     node *tree;
  2136. {
  2137.     int nch = NCH(tree);
  2138.     int res = (validate_ntype(tree, argument)
  2139.            && ((nch == 1) || (nch == 3))
  2140.            && validate_test(CHILD(tree, 0)));
  2141.  
  2142.     if (res && (nch == 3))
  2143.     res = (validate_equal(CHILD(tree, 1))
  2144.            && validate_test(CHILD(tree, 2)));
  2145.  
  2146.     return (res);
  2147.  
  2148. }   /* validate_argument() */
  2149.  
  2150.  
  2151.  
  2152. /*  trailer:
  2153.  *
  2154.  *  '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
  2155.  */
  2156. static int
  2157. validate_trailer(tree)
  2158.     node *tree;
  2159. {
  2160.     int nch = NCH(tree);
  2161.     int res = validate_ntype(tree, trailer) && ((nch == 2) || (nch == 3));
  2162.  
  2163.     if (res) {
  2164.     switch (TYPE(CHILD(tree, 0))) {
  2165.       case LPAR:
  2166.         res = validate_rparen(CHILD(tree, nch - 1));
  2167.         if (res && (nch == 3))
  2168.         res = validate_arglist(CHILD(tree, 1));
  2169.         break;
  2170.       case LSQB:
  2171.         res = (validate_numnodes(tree, 3, "trailer")
  2172.            && validate_subscriptlist(CHILD(tree, 1))
  2173.            && validate_ntype(CHILD(tree, 2), RSQB));
  2174.         break;
  2175.       case DOT:
  2176.         res = (validate_numnodes(tree, 2, "trailer")
  2177.            && validate_ntype(CHILD(tree, 1), NAME));
  2178.         break;
  2179.       default:
  2180.         res = 0;
  2181.         break;
  2182.     }
  2183.     }
  2184.     else
  2185.     validate_numnodes(tree, 2, "trailer");
  2186.  
  2187.     return (res);
  2188.  
  2189. }   /* validate_trailer() */
  2190.  
  2191.  
  2192. /*  subscriptlist:
  2193.  *
  2194.  *  subscript (',' subscript)* [',']
  2195.  */
  2196. static int
  2197. validate_subscriptlist(tree)
  2198.     node *tree;
  2199. {
  2200.     return (validate_repeating_list(tree, subscriptlist,
  2201.                     validate_subscript, "subscriptlist"));
  2202.  
  2203. }   /* validate_subscriptlist() */
  2204.  
  2205.  
  2206. /*  subscript:
  2207.  *
  2208.  *  '.' '.' '.' | test | [test] ':' [test] [sliceop]
  2209.  */
  2210. static int
  2211. validate_subscript(tree)
  2212.     node *tree;
  2213. {
  2214.     int offset = 0;
  2215.     int nch = NCH(tree);
  2216.     int res = validate_ntype(tree, subscript) && (nch >= 1) && (nch <= 4);
  2217.  
  2218.     if (!res) {
  2219.     if (!PyErr_Occurred())
  2220.         err_string("invalid number of arguments for subscript node");
  2221.     return (0);
  2222.     }
  2223.     if (TYPE(CHILD(tree, 0)) == DOT)
  2224.     /* take care of ('.' '.' '.') possibility */
  2225.     return (validate_numnodes(tree, 3, "subscript")
  2226.         && validate_dot(CHILD(tree, 0))
  2227.         && validate_dot(CHILD(tree, 1))
  2228.         && validate_dot(CHILD(tree, 2)));
  2229.     if (nch == 1) {
  2230.     if (TYPE(CHILD(tree, 0)) == test)
  2231.         res = validate_test(CHILD(tree, 0));
  2232.     else
  2233.         res = validate_colon(CHILD(tree, 0));
  2234.     return (res);
  2235.     }
  2236.     /*    Must be [test] ':' [test] [sliceop],
  2237.      *    but at least one of the optional components will
  2238.      *    be present, but we don't know which yet.
  2239.      */
  2240.     if ((TYPE(CHILD(tree, 0)) != COLON) || (nch == 4)) {
  2241.     res = validate_test(CHILD(tree, 0));
  2242.     offset = 1;
  2243.     }
  2244.     if (res)
  2245.     res = validate_colon(CHILD(tree, offset));
  2246.     if (res) {
  2247.     int rem = nch - ++offset;
  2248.     if (rem) {
  2249.         if (TYPE(CHILD(tree, offset)) == test) {
  2250.         res = validate_test(CHILD(tree, offset));
  2251.         ++offset;
  2252.         --rem;
  2253.         }
  2254.         if (res && rem)
  2255.         res = validate_sliceop(CHILD(tree, offset));
  2256.     }
  2257.     }
  2258.     return (res);
  2259.  
  2260. }   /* validate_subscript() */
  2261.  
  2262.  
  2263. static int
  2264. validate_sliceop(tree)
  2265.     node *tree;
  2266. {
  2267.     int nch = NCH(tree);
  2268.     int res = ((nch == 1) || validate_numnodes(tree, 2, "sliceop"))
  2269.           && validate_ntype(tree, sliceop);
  2270.     if (!res && !PyErr_Occurred()) {
  2271.     validate_numnodes(tree, 1, "sliceop");
  2272.     res = 0;
  2273.     }
  2274.     if (res)
  2275.     res = validate_colon(CHILD(tree, 0));
  2276.     if (res && (nch == 2))
  2277.     res = validate_test(CHILD(tree, 1));
  2278.  
  2279.     return (res);
  2280.  
  2281. }   /* validate_sliceop() */
  2282.  
  2283.  
  2284. static int
  2285. validate_exprlist(tree)
  2286.     node *tree;
  2287. {
  2288.     return (validate_repeating_list(tree, exprlist,
  2289.                     validate_expr, "exprlist"));
  2290.  
  2291. }   /* validate_exprlist() */
  2292.  
  2293.  
  2294. static int
  2295. validate_dictmaker(tree)
  2296.     node *tree;
  2297. {
  2298.     int nch = NCH(tree);
  2299.     int res = (validate_ntype(tree, dictmaker)
  2300.            && (nch >= 3)
  2301.            && validate_test(CHILD(tree, 0))
  2302.            && validate_colon(CHILD(tree, 1))
  2303.            && validate_test(CHILD(tree, 2)));
  2304.  
  2305.     if (res && ((nch % 4) == 0))
  2306.     res = validate_comma(CHILD(tree, --nch));
  2307.     else if (res)
  2308.     res = ((nch % 4) == 3);
  2309.  
  2310.     if (res && (nch > 3)) {
  2311.     int pos = 3;
  2312.     /*  ( ',' test ':' test )*  */
  2313.     while (res && (pos < nch)) {
  2314.         res = (validate_comma(CHILD(tree, pos))
  2315.            && validate_test(CHILD(tree, pos + 1))
  2316.            && validate_colon(CHILD(tree, pos + 2))
  2317.            && validate_test(CHILD(tree, pos + 3)));
  2318.         pos += 4;
  2319.     }
  2320.     }
  2321.     return (res);
  2322.  
  2323. }   /* validate_dictmaker() */
  2324.  
  2325.  
  2326. static int
  2327. validate_eval_input(tree)
  2328.     node *tree;
  2329. {
  2330.     int pos;
  2331.     int nch = NCH(tree);
  2332.     int res = (validate_ntype(tree, eval_input)
  2333.            && (nch >= 2)
  2334.            && validate_testlist(CHILD(tree, 0))
  2335.            && validate_ntype(CHILD(tree, nch - 1), ENDMARKER));
  2336.  
  2337.     for (pos = 1; res && (pos < (nch - 1)); ++pos)
  2338.     res = validate_ntype(CHILD(tree, pos), NEWLINE);
  2339.  
  2340.     return (res);
  2341.  
  2342. }   /* validate_eval_input() */
  2343.  
  2344.  
  2345. static int
  2346. validate_node(tree)
  2347.     node *tree;
  2348. {
  2349.     int   nch  = 0;            /* num. children on current node  */
  2350.     int   res  = 1;            /* result value              */
  2351.     node* next = 0;            /* node to process after this one */
  2352.  
  2353.     while (res & (tree != 0)) {
  2354.     nch  = NCH(tree);
  2355.     next = 0;
  2356.     switch (TYPE(tree)) {
  2357.         /*
  2358.          *  Definition nodes.
  2359.          */
  2360.       case funcdef:
  2361.         res = validate_funcdef(tree);
  2362.         break;
  2363.       case classdef:
  2364.         res = validate_class(tree);
  2365.         break;
  2366.         /*
  2367.          *  "Trivial" parse tree nodes.
  2368.          *  (Why did I call these trivial?)
  2369.          */
  2370.       case stmt:
  2371.         res = validate_stmt(tree);
  2372.         break;
  2373.       case small_stmt:
  2374.         /*
  2375.          *  expr_stmt | print_stmt  | del_stmt | pass_stmt | flow_stmt
  2376.          *  | import_stmt | global_stmt | exec_stmt
  2377.          */
  2378.         res = validate_small_stmt(tree);
  2379.         break;
  2380.       case flow_stmt:
  2381.         res  = (validate_numnodes(tree, 1, "flow_stmt")
  2382.             && ((TYPE(CHILD(tree, 0)) == break_stmt)
  2383.             || (TYPE(CHILD(tree, 0)) == continue_stmt)
  2384.             || (TYPE(CHILD(tree, 0)) == return_stmt)
  2385.             || (TYPE(CHILD(tree, 0)) == raise_stmt)));
  2386.         if (res)
  2387.         next = CHILD(tree, 0);
  2388.         else if (nch == 1)
  2389.         err_string("Illegal flow_stmt type.");
  2390.         break;
  2391.         /*
  2392.          *  Compound statements.
  2393.          */
  2394.       case simple_stmt:
  2395.         res = validate_simple_stmt(tree);
  2396.         break;
  2397.       case compound_stmt:
  2398.         res = validate_compound_stmt(tree);
  2399.         break;
  2400.         /*
  2401.          *  Fundemental statements.
  2402.          */
  2403.       case expr_stmt:
  2404.         res = validate_expr_stmt(tree);
  2405.         break;
  2406.       case print_stmt:
  2407.         res = validate_print_stmt(tree);
  2408.         break;
  2409.       case del_stmt:
  2410.         res = validate_del_stmt(tree);
  2411.         break;
  2412.       case pass_stmt:
  2413.         res = (validate_numnodes(tree, 1, "pass")
  2414.            && validate_name(CHILD(tree, 0), "pass"));
  2415.         break;
  2416.       case break_stmt:
  2417.         res = (validate_numnodes(tree, 1, "break")
  2418.            && validate_name(CHILD(tree, 0), "break"));
  2419.         break;
  2420.       case continue_stmt:
  2421.         res = (validate_numnodes(tree, 1, "continue")
  2422.            && validate_name(CHILD(tree, 0), "continue"));
  2423.         break;
  2424.       case return_stmt:
  2425.         res = validate_return_stmt(tree);
  2426.         break;
  2427.       case raise_stmt:
  2428.         res = validate_raise_stmt(tree);
  2429.         break;
  2430.       case import_stmt:
  2431.         res = validate_import_stmt(tree);
  2432.         break;
  2433.       case global_stmt:
  2434.         res = validate_global_stmt(tree);
  2435.         break;
  2436.       case exec_stmt:
  2437.         res = validate_exec_stmt(tree);
  2438.         break;
  2439.       case if_stmt:
  2440.         res = validate_if(tree);
  2441.         break;
  2442.       case while_stmt:
  2443.         res = validate_while(tree);
  2444.         break;
  2445.       case for_stmt:
  2446.         res = validate_for(tree);
  2447.         break;
  2448.       case try_stmt:
  2449.         res = validate_try(tree);
  2450.         break;
  2451.       case suite:
  2452.         res = validate_suite(tree);
  2453.         break;
  2454.         /*
  2455.          *  Expression nodes.
  2456.          */
  2457.       case testlist:
  2458.         res = validate_testlist(tree);
  2459.         break;
  2460.       case test:
  2461.         res = validate_test(tree);
  2462.         break;
  2463.       case and_test:
  2464.         res = validate_and_test(tree);
  2465.         break;
  2466.       case not_test:
  2467.         res = validate_not_test(tree);
  2468.         break;
  2469.       case comparison:
  2470.         res = validate_comparison(tree);
  2471.         break;
  2472.       case exprlist:
  2473.         res = validate_exprlist(tree);
  2474.         break;
  2475.       case comp_op:
  2476.         res = validate_comp_op(tree);
  2477.         break;
  2478.       case expr:
  2479.         res = validate_expr(tree);
  2480.         break;
  2481.       case xor_expr:
  2482.         res = validate_xor_expr(tree);
  2483.         break;
  2484.       case and_expr:
  2485.         res = validate_and_expr(tree);
  2486.         break;
  2487.       case shift_expr:
  2488.         res = validate_shift_expr(tree);
  2489.         break;
  2490.       case arith_expr:
  2491.         res = validate_arith_expr(tree);
  2492.         break;
  2493.       case term:
  2494.         res = validate_term(tree);
  2495.         break;
  2496.       case factor:
  2497.         res = validate_factor(tree);
  2498.         break;
  2499.       case power:
  2500.         res = validate_power(tree);
  2501.         break;
  2502.       case atom:
  2503.         res = validate_atom(tree);
  2504.         break;
  2505.  
  2506.       default:
  2507.         /* Hopefully never reached! */
  2508.         err_string("Unrecogniged node type.");
  2509.         res = 0;
  2510.         break;
  2511.     }
  2512.     tree = next;
  2513.     }
  2514.     return (res);
  2515.  
  2516. }   /* validate_node() */
  2517.  
  2518.  
  2519. static int
  2520. validate_expr_tree(tree)
  2521.     node *tree;
  2522. {
  2523.     int res = validate_eval_input(tree);
  2524.  
  2525.     if (!res && !PyErr_Occurred())
  2526.     err_string("Could not validate expression tuple.");
  2527.  
  2528.     return (res);
  2529.  
  2530. }   /* validate_expr_tree() */
  2531.  
  2532.  
  2533. /*  file_input:
  2534.  *    (NEWLINE | stmt)* ENDMARKER
  2535.  */
  2536. static int
  2537. validate_file_input(tree)
  2538.     node *tree;
  2539. {
  2540.     int j   = 0;
  2541.     int nch = NCH(tree) - 1;
  2542.     int res = ((nch >= 0)
  2543.            && validate_ntype(CHILD(tree, nch), ENDMARKER));
  2544.  
  2545.     for ( ; res && (j < nch); ++j) {
  2546.     if (TYPE(CHILD(tree, j)) == stmt)
  2547.         res = validate_stmt(CHILD(tree, j));
  2548.     else
  2549.         res = validate_newline(CHILD(tree, j));
  2550.     }
  2551.     /*    This stays in to prevent any internal failues from getting to the
  2552.      *    user.  Hopefully, this won't be needed.  If a user reports getting
  2553.      *    this, we have some debugging to do.
  2554.      */
  2555.     if (!res && !PyErr_Occurred())
  2556.     err_string("VALIDATION FAILURE: report this to the maintainer!.");
  2557.  
  2558.     return (res);
  2559.  
  2560. }   /* validate_file_input() */
  2561.  
  2562.  
  2563. /*  Functions exported by this module.  Most of this should probably
  2564.  *  be converted into an AST object with methods, but that is better
  2565.  *  done directly in Python, allowing subclasses to be created directly.
  2566.  *  We'd really have to write a wrapper around it all anyway to allow
  2567.  *  inheritance.
  2568.  */
  2569. static PyMethodDef parser_functions[] =  {
  2570.     {"ast2tuple",    parser_ast2tuple,    1,
  2571.     "Creates a tuple-tree representation of an AST."},
  2572.     {"ast2list",    parser_ast2list,    1,
  2573.     "Creates a list-tree representation of an AST."},
  2574.     {"compileast",    parser_compileast,    1,
  2575.     "Compiles an AST object into a code object."},
  2576.     {"expr",        parser_expr,        1,
  2577.     "Creates an AST object from an expression."},
  2578.     {"isexpr",        parser_isexpr,        1,
  2579.     "Determines if an AST object was created from an expression."},
  2580.     {"issuite",        parser_issuite,        1,
  2581.     "Determines if an AST object was created from a suite."},
  2582.     {"suite",        parser_suite,        1,
  2583.     "Creates an AST object from a suite."},
  2584.     {"sequence2ast",    parser_tuple2ast,    1,
  2585.     "Creates an AST object from a tree representation."},
  2586.     {"tuple2ast",    parser_tuple2ast,    1,
  2587.     "Creates an AST object from a tree representation."},
  2588.  
  2589.     {0, 0, 0}
  2590.     };
  2591.  
  2592.  
  2593. void
  2594. initparser()
  2595.  {
  2596.     PyObject* module = Py_InitModule("parser", parser_functions);
  2597.     PyObject* dict   = PyModule_GetDict(module);
  2598.  
  2599.     parser_error = PyString_FromString("parser.ParserError");
  2600.  
  2601.     if ((parser_error == 0)
  2602.     || (PyDict_SetItemString(dict, "ParserError", parser_error) != 0)) {
  2603.     /*
  2604.      *  This is serious.
  2605.      */
  2606.     Py_FatalError("can't define parser.ParserError");
  2607.     }
  2608.     /*
  2609.      *  Nice to have, but don't cry if we fail.
  2610.      */
  2611.     Py_INCREF(&PyAST_Type);
  2612.     PyDict_SetItemString(dict, "ASTType", (PyObject*)&PyAST_Type);
  2613.  
  2614.     PyDict_SetItemString(dict, "__copyright__",
  2615.              PyString_FromString(parser_copyright_string));
  2616.     PyDict_SetItemString(dict, "__doc__",
  2617.              PyString_FromString(parser_doc_string));
  2618.     PyDict_SetItemString(dict, "__version__",
  2619.              PyString_FromString(parser_version_string));
  2620.  
  2621. }   /* initparser() */
  2622.  
  2623.  
  2624. /*
  2625.  *  end of parsermodule.c
  2626.  */
  2627.